﻿
#from django.forms.formsets import formset_factory
#from django.forms.models import inlineformset_factory
from django.forms import ModelForm
from django import forms
from django.contrib.auth.models import User
from homesite.bugs.models import Project, Task, Tag, History
from homesite.bugs import fields



BUG_RESOLUTION_CHOICES = (
    ('Fixed', 'Fixed'),
    ('Canceled', 'Canceled'),
    ('Invalid', 'Invalid'),
    ('Same as', 'Same as'),
)



FEATURE_RESOLUTION_CHOICES = (
    ('Implemented', 'Implemented'),
    ('Denided', 'Denided'),
    ('Invalid', 'Invalid'),
    ('Same as', 'Same as'),
)



class TaskForm(ModelForm): 

    def __init__(self, project_id, *args, **kwargs):
        super(TaskForm, self).__init__(*args, **kwargs)
        user_queryset = User.objects.filter(project__id=project_id, is_active=True)  
        self.fields['subscribers'].queryset = user_queryset 
        self.fields['owner'].queryset = user_queryset
        self.fields['reporter'].queryset = user_queryset  

    project =  forms.ModelChoiceField(Project.objects.all(), widget=forms.HiddenInput)
    kind =  forms.CharField(widget=forms.HiddenInput)
    status =  forms.CharField(widget=forms.HiddenInput)
    summary = forms.CharField(max_length=100, widget=forms.TextInput(attrs={'size':'80'}))
    description = forms.CharField(widget=forms.Textarea(attrs={'cols':'61','rows':'5'}), required=False)
    reporter = forms.ModelChoiceField(None, widget=forms.HiddenInput)
    owner = forms.ModelChoiceField(None, empty_label=None) 
    subscribers =  forms.ModelMultipleChoiceField(None, widget=forms.CheckboxSelectMultiple, required=False)
 
    class Meta:
        model = Task



class HistoryForm(TaskForm):
    comment = forms.CharField(widget=forms.Textarea(attrs={'cols':'61','rows':'5'}), required=False)
    attachment = forms.FileField(required=False)



class TaskBugHistoryForm(HistoryForm):                                
    resolution  = forms.ChoiceField(required=False, choices=BUG_RESOLUTION_CHOICES)



class TaskFeatureHistoryForm(HistoryForm):
    resolution = forms.ChoiceField(required=False, choices=FEATURE_RESOLUTION_CHOICES)

    
    
class TagForm(ModelForm):

    class Meta:
        model = Tag